<?php
/**
 * Description: 购课邀请码校验
 * User: devforma
 * Date: 15/9/28
 * Time: 16:40
 */

class ValidateInviteCode implements iController {

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        global $cmsdb;

        if (validateUser($queryArgs) !== 1 || empty($queryArgs['invite_code']))
            return genOutput(1000);

        $userId = hexdec($queryArgs['invite_code']);

        //获取所有全程班的id
        $courseIds = $cmsdb->getCol('SELECT `id` FROM `data_live_course` WHERE `can_invite`=1');
        $idsStr = implode(',', $courseIds);

        $exists = $cmsdb->getVar('SELECT `id` FROM `relation_user_product` WHERE `user_id`=:uid AND `product_id` IN ('.
            $idsStr.') AND `product_type`="zhiboke" LIMIT 1', array(':uid' => $userId));

        return genOutput(1, array(
            'is_valid' => $exists ? true : false
        ));
    }
}